//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.crop.dao.QuestionDao;
import com.qst.crop.entity.Question;
import com.qst.crop.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionDao questionDao;
    private Integer pageSize = 30;

    public QuestionServiceImpl() {
    }

    public Question selectById(Integer id) {
        Question question = this.questionDao.selectByPrimaryKey(id);
        return question;
    }

    public List<Question> selectByQuestion(String type) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Question question = new Question();
        if ("questioner".equals(type)) {
            question.setQuestioner(name);
        } else {
            question.setExpertName(name);
        }

        List<Question> questions = this.questionDao.selectByQuestion(question);
        return questions;
    }

    public void delete(Integer id) {
        this.questionDao.deleteByPrimaryKey(id);
    }

    public void insert(Question question) {
        try{
            UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String name = principal.getUsername();
            question.setQuestioner(name);
            this.questionDao.insertSelective(question);
        }catch(Exception e){
            System.out.println("123error:" + e);
        }


    }

    public void updateById(Question question) {
        this.questionDao.updateByPrimaryKeySelective(question);
    }

    public PageInfo<Question> selectByKeys(String keys, Integer pageNum) {
        PageHelper.startPage(pageNum, this.pageSize);
        List<Question> questions = this.questionDao.selectAllByKeys(keys);
        PageInfo<Question> questionPageInfo = new PageInfo(questions);
        return questionPageInfo;
    }
}
